package com.jd.jdt.std.yfk.admin.controller;


import com.alibaba.fastjson.JSONObject;
import com.jd.jdt.std.yfk.admin.result.AjaxResult;
import com.jd.jdt.std.yfk.admin.service.workbenches.WorkbenchesService;
import com.jd.jdt.std.yfk.admin.service.workbenches.vo.FormInfoBean;
import com.jd.jdt.std.yfk.admin.service.workbenches.vo.UserProductInfoVo;
import com.jd.jdt.std.yfk.admin.service.workbenches.vo.WorkbenchesItemVo;
import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户信息看板
 */
@Controller
@Slf4j
public class UserWorkbenchesController {

    @Resource
    WorkbenchesService workbenchesService;
    @GetMapping("/index")
    public String showInfo(ModelMap mmap, HttpServletRequest request){
        return "index";
    }


    @PostMapping("/userWorkbenches/getUserProductCode")
    @ResponseBody
    public AjaxResult getUserProductCode(FormInfoBean formInfoBean)
    {
        log.info("getUserProductCode-表单信息:{}", JSONObject.toJSONString(formInfoBean));
        List<UserProductInfoVo> userProductInfoVoList = workbenchesService.getUserProduct(formInfoBean);
        if (userProductInfoVoList == null){
            return AjaxResult.error("获取用户信息失败，非采购用户！");
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("products",userProductInfoVoList);
        return ajax;
    }

    @PostMapping("/userWorkbenches/getUserProductInfo")
    @ResponseBody
    public AjaxResult getUserProductInfo(FormInfoBean formInfoBean)
    {
        log.info("getUserProductInfo-表单信息:{}", JSONObject.toJSONString(formInfoBean));
        List<UserProductInfoVo> userProductInfoVoList = workbenchesService.getUserProduct(formInfoBean);
        if (userProductInfoVoList == null){
            return AjaxResult.error("获取用户产品信息失败！");
        }
        List<WorkbenchesItemVo> userProductInfo =   workbenchesService.getUserProductInfo(userProductInfoVoList.get(0));
        if (userProductInfo == null){
            return AjaxResult.error("获取用户产品信息失败！");
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("userInfo",workbenchesService.buildHtml(userProductInfo));
        return ajax;
    }
}
